define(function(require) {
	var $ = require("jquery");
	var justep = require("$UI/system/lib/justep");
	var util = require("./util/util");
	var Model = function() {
		this.callParent();
		this.orderbyValue = [ {
			index : 0,
			key : "com.qq.weixin.open.appID"
		}, {
			index : 1,
			key : "com.qq.weixin.open.appSecret"
		}, {
			index : 2,
			key : "com.qq.weixin.open.token"
		}, {
			index : 3,
			key : "com.qq.weixin.open.encodingAESKey"
		}];
	};

	Model.prototype.saveBtnClick = function(event) {
		var self = this;
		var platformData = this.comp("platformData");
		var option = platformData.toJson({
			"format" : "simple"
		}).rows;
		option.forEach(function(item, index) {
			delete item.userdata;
			if(item.fvalue && typeof(item.fvalue)!="object"){
			    item.fvalue.trim();
			}
		});
		if(option.length === 5){
			option.pop();
		}
		$.ajax({
			url : "/config/config/config",
			type : "POST",
			contentType : "application/json",
			datatype : "json",
			data : JSON.stringify(option),
			success : function(res) {
				justep.Util.hint("保存成功");
				self.comp("platformData").loadData(res);
			},
			error : function(err) {
				justep.Util.hint("保存失败，请稍后重试！", {
					"type" : "warning"
				});
			}
		});
	};
	Model.prototype.wxPublicDataDefault = function(event) {
		var platformData = this.comp("platformData");
		var groupName = "default" + "_open";// + getGroupId();
		var options = [ {
			"id" : "wx11",
			"fkey" : "com.qq.weixin.open.appID",
			"fvalue" : '',
			"fgroup" : groupName,
			"flabel" : "appID",
			issecure : 0
		}, {
			"id" : "wx12",
			"fkey" : "com.qq.weixin.open.appSecret",
			"fvalue" : '',
			"fgroup" : groupName,
			"flabel" : "appSecret",
			issecure : 0
		}, {
			"id" : "wx13",
			"fkey" : "com.qq.weixin.open.token",
			"fvalue" : '',
			"fgroup" : groupName,
			"flabel" : "消息事件消息加解密token",
			issecure : 0
		}, {
			"id" : "wx14",
			"fkey" : "com.qq.weixin.open.encodingAESKey",
			"fvalue" : '',
			"fgroup" : groupName,
			"flabel" : "消息事件消息加解密encodingAESKey",
			issecure : 0
		}, {
			"id" : "",
			"fkey" : "com.qq.weixin.open.orderby",
			"fvalue" : this.orderbyValue,
			"fgroup" : groupName,
			"flabel" : "排序",
			issecure : 0
		}];
		platformData.newData({
			"defaultValues" : options
		});
	};
	Model.prototype.platformDataCustomRefresh = function(event) {
		var self = this;
		$.ajax({
			url : "/config/config/configs/search/like",
			type : "GET",
			data : {
				"key" : "com.qq.weixin.open",
				"group" : "default_open"
			},
			success : function(res) {
				// 利用ID给返回的数据重新排序
				if(res.length === 0){
					self.wxPublicDataDefault();
				}else{
					if (res.length == 4) {
						res.push({
							"id" : "",
							"fkey" : "com.qq.weixin.open.orderby",
							"fvalue" : self.orderbyValue,
							"fgroup" : "default_open",
							"flabel" : "排序",
							issecure : 0
						});
					}
					res = util.sort(res,"com.qq.weixin.open.orderby");
					self.comp("platformData").loadData(res);
				}
			},
			error : function(err) {
				self.wxPublicDataDefault();
			}
		});
	};

	return Model;
});